package com.jlkf.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.github.pagehelper.PageHelper;
import com.jlkf.dao.CityRecordMapper;
import com.jlkf.dao.ConfigManagerMapper;
import com.jlkf.pojo.CityRecord;
import com.jlkf.pojo.ConfigManager;
import com.jlkf.pojo.SugFeedback;
import com.jlkf.service.ConfigManagerService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

import net.sf.json.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service
public class ConfigManagerServiceImpl implements ConfigManagerService{

	@Autowired
	private ConfigManagerMapper configManagerMapper;
	@Autowired
	private CityRecordMapper cityRecordMapper;
	@Autowired
	private JedisPool jedisPool;
	@Override
	public Object getList() {
		try {
			 PageHelper.startPage(1, 1);
			 List<ConfigManager> list=configManagerMapper.getList();
			 return new ReturnData(list,200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

	
	@Override
	public Object addOrUpdate(String json) {
		ConfigManager cm = StringTools.json2Obj(json, ConfigManager.class);
		if(cm==null){
			return new ReturnData(201,"The parameter list is empty!");
		}
		Jedis jedis = null;
		if(jedis==null){
			jedis =jedisPool.getResource();
		}
		try {
			int num=configManagerMapper.updateByPrimaryKeySelective(cm);
			if(num==0){
				return new ReturnData(202,"fail");
			}
			 cm = configManagerMapper.selectByPrimaryKey(1);
			 JSONObject jo = JSONObject.fromObject(cm);
			 jedis.del("configManager");
			jedis.set("configManager", jo.toString());
				return new ReturnData(200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}finally {
			if(jedis!=null){
   				jedis.close();
   			}
		}
	}

  //城市列表
	@Override
	public Object getCityRecord() {
		Map<String,Object> map = new HashMap<>();
		try {
			List<CityRecord> list=cityRecordMapper.getList(map);
			return new ReturnData(list,200,"success");
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

   //保存司机服务时间
	@Override
	public Object saveServiceTime(String theServiceTimes, String ids) {
		if(StringTools.isEmpty(ids)){
			return new ReturnData(201,"The city id cannot be empty.");
		}
		if(StringTools.isEmpty(theServiceTimes)){
			return new ReturnData(202,"Service time must be filled.");
		}
		int code=200;boolean flag=true;String msg="success";
		try {
			String[] idList = ids.split(",");
			String[] theServiceTimess = theServiceTimes.split(",");
			for(int i=0;i<theServiceTimess.length;i++){
				String st=theServiceTimess[i];
				String id=idList[i];
				if(!StringTools.isEmpty(st)){
					CityRecord cr = new CityRecord();
					cr.setId(Long.parseLong(id));
					cr.setTheServiceTime(Integer.parseInt(st));
				int n=cityRecordMapper.updateByPrimaryKeySelective(cr);
				   if(n==0){
					   flag=false; code=203; msg="fail";
					   break;
				   }
				}
			}
			if(!flag){
				TransactionAspectSupport.currentTransactionStatus().isRollbackOnly();
			}
			return new ReturnData(code,msg);
		} catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"Wow, that's wrong");
		}
	}

}
